<template>
  <view>
    <view class="login-container">
      <!-- 提示登录的图标 -->
      <uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
      <!-- 登录按钮 -->
      <button type="primary" class="btn-login" open-type="getUserInfo" @getuserinfo="getUserProfile">一键登录</button>
      <!-- 登录提示 -->
      <text class="tips-login">登录后尽享权益</text>

    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations
  } from "vuex"
  export default {
    name: "my-login",
    data() {
      return {

      };
    },
    computed: {
      ...mapState('m_user', ['redirectInfo'])
    },
    methods: {
      ...mapMutations('m_user', ['updateUserInfo', 'updateToken', 'updateRedirectInfo']),
      // 定义获取用户信息的方法
      getUserInfo(e) {
        if (e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权！')
        this.updateUserInfo(e.detail.userInfo)
        // 获取登录成功后的token字符串
        this.getToken(e.detail);
      },
      // 获取微信用户的基本信息
      getUserProfile(e) {
        if (e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权！')
        this.updateUserInfo(e.detail.userInfo)
        // 获取登录成功后的token字符串
        this.getToken(e.detail);
      },
      // 定义登录后跳转回原页面的方法
      navigateBack() {
        if (this.redirectInfo && this.redirectInfo.openType === 'switchTab')
          uni.switchTab({
            url: this.redirectInfo.from,
            complete: () => {
              this.updateRedirectInfo(null)
            }
          })
      },
      // 定义获取token字符串的方法
      async getToken(info) {
        const [err, res] = await uni.login().catch(err => err)
        if (err || res.errMsg !== 'login:ok') return uni.$showMsg('登录失败！')

        // 准备请求参数对象
        const query = {
          code: res.code,
          encryptedData: info.encryptedData,
          iv: info.iv,
          rawData: info.rawData,
          signature: info.signature
        }

        // 发送网络请求换取token
        const {
          data: loginResult
        } = await uni.$http.post('/api/public/v1/users/wxlogin', query)
        console.log(loginResult)
        if (loginResult.meta.status !== 200) {
          uni.$showMsg('登录失败！')
          // 这种伪造方法太低劣了
          loginResult.message = {
            "user_id": 12,
            "user_email_code": null,
            "is_active": null,
            "user_sex": "男",
            "user_qq": "",
            "user_tel": "",
            "user_xueli": "本科",
            "user_hobby": "",
            "user_introduce": null,
            "create_time": 1525402223,
            "update_time": 1525402223,
            "token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEyLCJpYXQiOjE1MjU0MDIyMjMsImV4cCI6MTUyNTQ4ODYyM30.g-4GtEQNPwT_Xs0Pq7Lrco_9DfHQQsBiOKZerkO-O-o"
          }
        }
        // 登录成功则更新 vuex 中的 token(由于没接口所以无法登录成功只能伪造)
        this.updateToken(loginResult.message.token)
        // 重新导航回跳转过来的页面
        this.navigateBack();
      }
    }
  }
</script>

<style lang="scss">
  .login-container {
    height: 750rpx;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #F8F8F8;
    position: relative;
    overflow: hidden;

    // 绘制盒子底部的半椭圆造型
    &::after {
      content: '';
      display: block;
      position: absolute;
      width: 100%;
      height: 40px;
      background-color: #FFFFFF;
      border-radius: 100%;
      bottom: 0;
      left: 0;
      transform: translateY(50%);
    }

    .btn-login {
      width: 90%;
      background-color: #C00000;
      border-radius: 100px;
      margin: 15px 0;
    }

    .tips-login {
      font-size: 12px;
      color: gray;
    }
  }
</style>
